package com.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.DepDAO;
import com.pojo.Dep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepService extends ServiceImpl<DepDAO, Dep> {

    @Autowired
    private DepDAO depDAO;

    @Autowired
    private RedisTemplate redisTemplate;

    @Cacheable(value = "dep",key = "'all'")
    public List findAll()
    {
        return depDAO.selectList(null);
    }

    /**
     * 先把缓存里面查 dep::1,如果找不到则直接返回，如果没有找到，则到数据库查，查到存缓存redis
     * @param id
     * @return
     */
    @Cacheable(value = "dep",key="#id")
    public Dep findById(int id)
    {
        return depDAO.selectById(id);
    }

    /**
     * 删除数据库前，先删除缓存
     * @param id
     * @return
     */
    @CacheEvict(value = "dep",key="#id",beforeInvocation = true)
    public int delById(int id)
    {
        return depDAO.deleteById(id);
    }


    /**
     * 存数据库
     * 再存缓存
     * @param dep
     * @return
     */
    @CachePut(value = "dep",key="#dep.depId")
    public int updateDep(Dep dep)
    {
        return depDAO.updateById(dep);
    }


}
